package com.example.dw.admin.controller;

import com.example.dw.common.Result;
import com.example.dw.admin.service.AdminHomeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 管理端首页控制器
 * @author system
 * @since 2024-01-01
 */
@Tag(name = "管理端首页", description = "首页统计数据和系统信息接口")
@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminHomeController {

    @Autowired
    private AdminHomeService adminHomeService;

    /**
     * 获取今日订单
     */
    @Operation(summary = "获取今日订单", description = "获取今日订单统计数据")
    @GetMapping("/orderByDay")
    public Result<?> getOrderByDay() {
        try {
            Map<String, Object> result = adminHomeService.getOrderByDay();
            return Result.success("获取今日订单成功", result);
        } catch (Exception e) {
            log.error("获取今日订单失败: {}", e.getMessage());
            return Result.error("获取今日订单失败");
        }
    }

    /**
     * 获取几个订单
     */
    @Operation(summary = "获取几个订单", description = "获取指定数量的最新订单")
    @GetMapping("/orderBySome")
    public Result<?> getOrderBySome(@RequestParam(defaultValue = "10") Integer limit) {
        try {
            Map<String, Object> result = adminHomeService.getOrderBySome(limit);
            return Result.success("获取订单列表成功", result);
        } catch (Exception e) {
            log.error("获取订单列表失败: {}", e.getMessage());
            return Result.error("获取订单列表失败");
        }
    }

    /**
     * 获取总订单
     */
    @Operation(summary = "获取总订单", description = "获取订单总数统计")
    @GetMapping("/orderByAll")
    public Result<?> getOrderByAll() {
        try {
            Map<String, Object> result = adminHomeService.getOrderByAll();
            return Result.success("获取总订单成功", result);
        } catch (Exception e) {
            log.error("获取总订单失败: {}", e.getMessage());
            return Result.error("获取总订单失败");
        }
    }

    /**
     * 获取今日新增用户
     */
    @Operation(summary = "获取今日新增用户", description = "获取今日新增用户统计")
    @GetMapping("/userByDay")
    public Result<?> getUserByDay() {
        try {
            Map<String, Object> result = adminHomeService.getUserByDay();
            return Result.success("获取今日新增用户成功", result);
        } catch (Exception e) {
            log.error("获取今日新增用户失败: {}", e.getMessage());
            return Result.error("获取今日新增用户失败");
        }
    }

    /**
     * 获取总用户
     */
    @Operation(summary = "获取总用户", description = "获取用户总数统计")
    @GetMapping("/userByAll")
    public Result<?> getUserByAll() {
        try {
            Map<String, Object> result = adminHomeService.getUserByAll();
            return Result.success("获取总用户成功", result);
        } catch (Exception e) {
            log.error("获取总用户失败: {}", e.getMessage());
            return Result.error("获取总用户失败");
        }
    }

    /**
     * 获取总课程
     */
    @Operation(summary = "获取总课程", description = "获取课程总数统计")
    @GetMapping("/courseByAll")
    public Result<?> getCourseByAll() {
        try {
            Map<String, Object> result = adminHomeService.getCourseByAll();
            return Result.success("获取总课程成功", result);
        } catch (Exception e) {
            log.error("获取总课程失败: {}", e.getMessage());
            return Result.error("获取总课程失败");
        }
    }

    /**
     * 获取总题库
     */
    @Operation(summary = "获取总题库", description = "获取题库总数统计")
    @GetMapping("/questByAll")
    public Result<?> getQuestByAll() {
        try {
            Map<String, Object> result = adminHomeService.getQuestByAll();
            return Result.success("获取总题库成功", result);
        } catch (Exception e) {
            log.error("获取总题库失败: {}", e.getMessage());
            return Result.error("获取总题库失败");
        }
    }

    /**
     * 获取一周或一个月订单
     */
    @Operation(summary = "获取一周或一个月订单", description = "获取指定时间段的订单统计")
    @PostMapping("/orDerByWOrM")
    public Result<?> getOrDerByWOrM(@RequestBody Map<String, Object> params) {
        try {
            Map<String, Object> result = adminHomeService.getOrDerByWOrM(params);
            return Result.success("获取订单统计成功", result);
        } catch (Exception e) {
            log.error("获取订单统计失败: {}", e.getMessage());
            return Result.error("获取订单统计失败");
        }
    }

    /**
     * 用户协议
     */
    @Operation(summary = "获取用户协议", description = "获取用户协议内容")
    @GetMapping("/getPack")
    public Result<?> getPack() {
        try {
            Map<String, Object> result = adminHomeService.getPack();
            return Result.success("获取用户协议成功", result);
        } catch (Exception e) {
            log.error("获取用户协议失败: {}", e.getMessage());
            return Result.error("获取用户协议失败");
        }
    }

    /**
     * 关于我们
     */
    @Operation(summary = "获取关于我们", description = "获取关于我们内容")
    @GetMapping("/getAboutMe")
    public Result<?> getAboutMe() {
        try {
            Map<String, Object> result = adminHomeService.getAboutMe();
            return Result.success("获取关于我们成功", result);
        } catch (Exception e) {
            log.error("获取关于我们失败: {}", e.getMessage());
            return Result.error("获取关于我们失败");
        }
    }

    /**
     * 服务器状态
     */
    @Operation(summary = "获取服务器状态", description = "获取服务器运行状态信息")
    @GetMapping("/getServer")
    public Result<?> getServer() {
        try {
            Map<String, Object> result = adminHomeService.getServer();
            return Result.success("获取服务器状态成功", result);
        } catch (Exception e) {
            log.error("获取服务器状态失败: {}", e.getMessage());
            return Result.error("获取服务器状态失败");
        }
    }
} 